package com.ruoyi.system.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DabSupplier;
import com.ruoyi.system.service.IDabSupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据供应商信息Controller
 *
 * @author ruoyi
 * @date 2023-06-21
 */
@Controller
@RequestMapping("/system/supplier")
public class DabSupplierController extends BaseController {
  private String prefix = "system/supplier" ;

  @Autowired
  private IDabSupplierService dabSupplierService;

  @RequiresPermissions("system:supplier:view")
  @GetMapping()
  public String supplier() {
    return prefix + "/supplier" ;
  }

  /**
   * 查询数据供应商信息列表
   */
  @RequiresPermissions("system:supplier:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(DabSupplier dabSupplier) throws JsonProcessingException {
    om.writeValueAsString(dabSupplier);
    startPage();
    List<DabSupplier> list = dabSupplierService.selectByEntity(dabSupplier);
    return getDataTable(list);
  }

  /**
   * 导出数据供应商信息列表
   */
  @RequiresPermissions("system:supplier:export")
  @Log(title = "数据供应商信息" , businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(DabSupplier dabSupplier) {
    List<DabSupplier> list = dabSupplierService.selectByEntity(dabSupplier);
    ExcelUtil<DabSupplier> util = new ExcelUtil<DabSupplier>(DabSupplier.class);
    return util.exportExcel(list, "数据供应商信息数据");
  }

  /**
   * 新增数据供应商信息
   */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add" ;
  }

  /**
   * 新增保存数据供应商信息
   */
  @RequiresPermissions("system:supplier:add")
  @Log(title = "数据供应商信息" , businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(DabSupplier dabSupplier) {
    return toAjax(dabSupplierService.insert(dabSupplier));
  }

  /**
   * 修改数据供应商信息
   */
  @RequiresPermissions("system:supplier:edit")
  @GetMapping("/edit/{sid}")
  public String edit(@PathVariable("sid") Long sid, ModelMap mmap) {
    DabSupplier dabSupplier = dabSupplierService.selectById(sid);
    mmap.put("dabSupplier" , dabSupplier);
    return prefix + "/edit" ;
  }

  /**
   * 修改保存数据供应商信息
   */
  @RequiresPermissions("system:supplier:edit")
  @Log(title = "数据供应商信息" , businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(DabSupplier dabSupplier) {
    return toAjax(dabSupplierService.updateById(dabSupplier));
  }

  /**
   * 删除数据供应商信息
   */
  @RequiresPermissions("system:supplier:remove")
  @Log(title = "数据供应商信息" , businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(Long ids) {
    return toAjax(dabSupplierService.deleteById(ids));
  }
}
